package com.jmodata.zxu.controller.manager;

import com.jmodata.zxu.model.DataGrid;
import com.jmodata.zxu.model.Json;
import com.jmodata.zxu.model.PageHelper;
import com.jmodata.zxu.pojo.manager.Manager;
import com.jmodata.zxu.pojo.manager.ManagerRole;
import com.jmodata.zxu.pojo.manager.Role;
import com.jmodata.zxu.pojo.manager.RoleMenu;
import com.jmodata.zxu.service.manager.ManagerService;
import com.jmodata.zxu.service.manager.RoleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("role")
public class RoleController {

	@Autowired(required = false)
	private RoleService roleService;

	@Autowired(required = false)
	private ManagerService managerService;

	/**
	 * 跳转到role页面
	 * 
	 * @return
	 */
	@RequestMapping("")
	public String role() {
		return "role/role";
	}

	/**
	 * 跳转到添加页�?
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/toaddrole")
	public String toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "role/roleForm";
	}

	/**
	 * 跳转到授权页�?
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/togrant")
	public String toGrant(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "role/grant";
	}

	/**
	 * 通过菜单编号查询对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/getrole")
	@ResponseBody
	public Json getRole(HttpServletRequest request, String roleid) throws Exception {
		Json json = new Json();
		Role role = null;
		if (null != roleid && !"".equals(roleid)) {
			role = roleService.getRoleById(roleid);
			if (null != role) {
				json.setObj(role);
			} else {
				json.setSuccess(false);
				json.setMsg("查询角色失败!");
			}
		}
		return json;
	}

	/**
	 * 查询角色列表
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/roleList")
	@ResponseBody
	public DataGrid queryRoleList(HttpServletRequest request, PageHelper page, Role role) throws Exception {
		Manager manager = (Manager) request.getSession().getAttribute("mgr");
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("order", page.getOrder());
		map.put("sort", page.getSort());
		map.put("rows", page.getRows());
		map.put("start", page.getStart());
		map.put("role_name", role.getRole_name());
		map.put("manager_id", manager.getManager_id());
		return roleService.queryRoleList(map);
	}

	/**
	 * 添加角色
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @throws Exception
	 */
	@RequestMapping("/addrole")
	@ResponseBody
	public Json addRole(HttpServletRequest request, Role role) throws Exception {
		Json json = new Json();
		Manager manager = (Manager) request.getSession().getAttribute("mgr");
		role.setCreate_time(new Date());
		role.setManager_id(manager.getManager_id());
		int flag = roleService.insertRole(role);
		if (flag > 0) {
			json.setSuccess(true);
			json.setMsg("角色添加成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("角色添加失败!");
		}
		return json;
	}

	/**
	 * 修改角色
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @throws Exception
	 */
	@RequestMapping("/editrole")
	@ResponseBody
	public Json editRole(HttpServletRequest request, Role role) throws Exception {
		Json json = new Json();
		Manager manager = (Manager) request.getSession().getAttribute("mgr");
		role.setManager_id(manager.getManager_id());
		int flag = roleService.updateRole(role);
		if (flag > 0) {
			json.setSuccess(true);
			json.setMsg("角色修改成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("角色修改失败!");
		}
		return json;
	}

	/**
	 * 删除角色
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @throws Exception
	 */
	@RequestMapping("/delrole")
	@ResponseBody
	public Json delRole(HttpServletRequest request, String roleid) throws Exception {
		Json json = new Json();
		int flag = 0;
		if (null != roleid && !"".equals(roleid)) {
			flag = roleService.deleteRole(new BigDecimal(roleid));
			// 删除角色成功后，再删除该角色�?��的权�?
			roleService.deleteRoleMenu(roleid);
			// 删除管理员所拥有的角�?
			managerService.deleteManagerRole(Integer.parseInt(roleid));
		}
		if (flag > 0) {
			json.setSuccess(true);
			json.setMsg("角色删除成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("角色删除失败!");
		}
		return json;
	}

	/**
	 * 给角色授�?
	 * 
	 * @param request
	 * @param response
	 * @param roleid
	 * @param modules
	 * @throws Exception
	 */
	@RequestMapping("/grant")
	@ResponseBody
	public Json grant(HttpServletRequest request, String roleid, String modules) throws Exception {
		Json json = new Json();
		List<RoleMenu> list = new ArrayList<RoleMenu>();
		int flag = 0;
		if (null != modules && !"".equals(modules)) {
			String[] module = modules.split(",");
			if (module.length > 0) {
				for (int i = 0; i < module.length; i++) {
					RoleMenu roleMenu = new RoleMenu();
					roleMenu.setRole_id(new BigDecimal(roleid));
					roleMenu.setMenu_id(new BigDecimal(module[i]));
					list.add(roleMenu);
				}
			}
			if (null != roleid && !"".equals(roleid)) {
				if (null != list && list.size() > 0) {
					flag = roleService.insertRoleMenu(list, roleid);
				}
			}
		}
		if (flag > 0) {
			json.setSuccess(true);
			json.setMsg("角色授权成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("角色授权失败!");
		}
		return json;
	}

	/**
	 * 查询角色�?��有的权限
	 * 
	 * @param request
	 * @param response
	 * @param roleid
	 * @param modules
	 * @throws Exception
	 */
	@RequestMapping("/grantbyrole")
	public void grantByRole(HttpServletRequest request, HttpServletResponse response, String roleid) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		List<RoleMenu> list = new ArrayList<RoleMenu>();
		list = roleService.queryRoleMenu(roleid);
		if (null != list && list.size() > 0) {
			json.put("list", list);
			json.put("success", true);
		} else {
			json.put("list", "");
			json.put("success", false);
		}
		out.print(json);
	}

	/**
	 * 根据管理员id查询角色与管理员关联信息
	 * 
	 * @param request
	 * @param manager_id
	 * @return
	 */
	@RequestMapping("/findRoleIdByManagerId")
	@ResponseBody
	public String findRoleIdByManagerId(HttpServletRequest request, String manager_id) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("manager_id", manager_id);
		try {
			List<ManagerRole> list = roleService.findRoleIdByManagerId(map);
			JSONArray array = new JSONArray();
			return array.fromObject(list) + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "false";
	}

}
